home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************\
- * Console driver based on
- * the sample application from Inside Macintosh (RoadMap p.15-17)
- *
- \*******************************************************************/
-
- #include <Desk.h>
- #include <Scrap.h>
- #include <Fonts.h>
- #include <Memory.h>
- #include <OSEvents.h>
- #include <ToolUtils.h>
- #include <SegLoad.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Menus.h>
- #include <TextEdit.h>
-
- #define appleID 128
- #define fileID 129
- #define editID 130
-
- /* Edit menu command indices */
- #define undoCommand 1
- #define cutCommand 3
- #define copyCommand 4
- #define pasteCommand 5
- #define clearCommand 6
- #define selallCommand 7
-
- /* Menu indices */
- #define appleM 0
- #define fileM 1
- #define editM 2
-
- #define SBarWidth 15
-
- int DoCommand(long mResult);
-
- #include "crtlocal.h"
-
- static WindowRecord crt_wRecord;
- static int linesInFolder;
- static MenuHandle myMenus[3];
- static ControlHandle vScroll;
-
- #define ours(w) ((crt_myWindow != NULL) && (w == crt_myWindow))
-
- int SetUpWindows(Str255 name)
- {
- Rect viewRect;
- FontInfo myInfo;
- GrafPort myPort;
-
- OpenPort(&myPort);
- SetPort(&myPort);
-
- TextFont(monaco);
- TextSize(9);
- GetFontInfo(&myInfo);
- ClosePort(&myPort);
-
- viewRect.left = 0;
- viewRect.top = 32;
- viewRect.right = viewRect.left+82*myInfo.widMax+SBarWidth;
- viewRect.bottom = viewRect.top+25*(myInfo.ascent+myInfo.descent+myInfo.leading)+SBarWidth;
- OffsetRect(&viewRect,
- (qd.screenBits.bounds.right-viewRect.right)/2,
- (qd.screenBits.bounds.bottom-viewRect.bottom)/2);
- crt_myWindow = NewWindow(&crt_wRecord,&viewRect,name,1,0,(WindowPtr) -1L,1,0);
- SetPort(crt_myWindow);
- TextFont(monaco);
- TextSize(9);
-
- viewRect = qd.thePort->portRect;
- viewRect.left = viewRect.right-15;
- viewRect.right += 1;
- viewRect.bottom -= 14;
- viewRect.top -= 1;
- vScroll = NewControl( crt_myWindow, &viewRect, "\p", 1, 0, 0, 0, scrollBarProc, 0L);
-
- viewRect = qd.thePort->portRect;
- viewRect.right -= SBarWidth;
- viewRect.bottom -= SBarWidth;
- InsetRect(&viewRect, 4, 4);
-
- crt_TEH = TEStylNew(&viewRect, &viewRect);
- (**crt_TEH).viewRect = qd.thePort->portRect;
- (**crt_TEH).viewRect.right -= SBarWidth;
- (**crt_TEH).viewRect.bottom -= SBarWidth;
- InsetRect(&(**crt_TEH).viewRect, 4, 4);
-
- linesInFolder = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top)/(**crt_TEH).lineHeight;
- (**crt_TEH).viewRect.bottom = (**crt_TEH).viewRect.top + (**crt_TEH).lineHeight*linesInFolder;
- (**crt_TEH).destRect.right = (**crt_TEH).viewRect.right;
- TECalText(crt_TEH);
- }
-
- int AdjustText (void)
-
- {
- int oldScroll, newScroll, delta;
-
- oldScroll = (**crt_TEH).viewRect.top - (**crt_TEH).destRect.top;
- newScroll = GetCtlValue(vScroll) * (**crt_TEH).lineHeight;
- delta = oldScroll - newScroll;
- if (delta != 0)
- TEScroll(0, delta, crt_TEH);
- }
-
- #ifdef SCROLLPROC
-
- pascal void ScrollProc (ControlHandle theControl, int theCode)
- {
- int pageSize;
- int scrollAmt;
- int oldCtl;
-
- if (theCode == 0)
- return ;
-
- pageSize = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top) /
- (**crt_TEH).lineHeight - 1;
-
- switch (theCode) {
- case inUpButton:
- scrollAmt = -1;
- break;
- case inDownButton:
- scrollAmt = 1;
- break;
- case inPageUp:
- scrollAmt = -pageSize;
- break;
- case inPageDown:
- scrollAmt = pageSize;
- break;
- }
-
- oldCtl = GetCtlValue(theControl);
- SetCtlValue(theControl, oldCtl+scrollAmt);
-
- AdjustText();
-
- }
-
- #endif
-
- int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
-
- {
- switch (windowPart) {
- case inGoAway:
- if (ours(whichWindow))
- if (TrackGoAway(crt_myWindow, myEvent->where))
- {
- HideWindow(crt_myWindow);
- TESetSelect(0, (**crt_TEH).teLength, crt_TEH);
- TEDelete(crt_TEH);
- {
- register int n;
-
- n = (**crt_TEH).nLines-linesInFolder;
-
- if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(vScroll, n > 0 ? n : 0);
- }
- }
- break;
-
- case inMenuBar:
- return(DoCommand(MenuSelect(myEvent->where)));
-
- case inSysWindow:
- SystemClick(myEvent, whichWindow);
- break;
-
- case inDrag:
- if (ours(whichWindow))
- {
- Rect dragRect = { 0, 0, 1024, 1024 };
- DragWindow(whichWindow, myEvent->where, &dragRect);
- }
- break;
-
- case inGrow:
- if (ours(whichWindow))
- {
- Point p = myEvent->where;
- GrafPtr savePort;
- long theResult;
- Rect oldHorizBar;
- Rect r;
-
- GetPort(&savePort);
- SetPort(whichWindow);
-
- oldHorizBar = whichWindow->portRect;
- oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
-
- SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
- theResult = GrowWindow(whichWindow, p, &r);
- if (theResult == 0)
- return;
- SizeWindow( whichWindow, LoWord(theResult), HiWord(theResult), false);
-
- InvalRect(&whichWindow->portRect);
-
- (**crt_TEH).viewRect = whichWindow->portRect;
- (**crt_TEH).viewRect.right -= SBarWidth;
- (**crt_TEH).viewRect.bottom -= SBarWidth;
- InsetRect(&(**crt_TEH).viewRect, 4, 4);
-
- linesInFolder = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top)/(**crt_TEH).lineHeight;
- (**crt_TEH).viewRect.bottom = (**crt_TEH).viewRect.top + (**crt_TEH).lineHeight*linesInFolder;
- (**crt_TEH).destRect.right = (**crt_TEH).viewRect.right;
- TECalText(crt_TEH);
-
- EraseRect(&oldHorizBar);
-
- MoveControl(vScroll, whichWindow->portRect.right - SBarWidth, whichWindow->portRect.top-1);
- SizeControl(vScroll, SBarWidth+1, whichWindow->portRect.bottom - whichWindow->portRect.top-(SBarWidth-2));
- r = (**vScroll).contrlRect;
- ValidRect(&r);
-
-
- {
- register int n;
-
- n = (**crt_TEH).nLines-linesInFolder;
-
- if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(vScroll, n > 0 ? n : 0);
- }
-
- AdjustText();
-
- SetPort(savePort);
- }
- break;
-
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else if (ours(whichWindow))
- {
- int cntlCode;
- ControlHandle theControl;
- int pageSize;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(whichWindow);
-
- GlobalToLocal(&myEvent->where);
- if ((cntlCode = FindControl(myEvent->where, whichWindow, &theControl)) == 0) {
- if (PtInRect(myEvent->where, &(**crt_TEH).viewRect))
- TEClick(myEvent->where, (myEvent->modifiers & shiftKey)!=0, crt_TEH);
- } else if (cntlCode == inThumb) {
- TrackControl(theControl, myEvent->where, 0L);
- AdjustText();
- } else
- TrackControl(theControl, myEvent->where,
- #ifdef SCROLLPROC
- &ScrollProc);
- #else
- 0);
- #endif
- SetPort(savePort);
- }
- break;
- }
- }
-
-
- int ShowSelect (void)
-
- {
- register int topLine, bottomLine, theLine;
-
- register int n;
-
- n = (**crt_TEH).nLines-linesInFolder;
-
- if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(vScroll, n > 0 ? n : 0);
- AdjustText();
-
- topLine = GetCtlValue(vScroll);
- bottomLine = topLine + linesInFolder;
-
- if ((**crt_TEH).selStart < (**crt_TEH).lineStarts[topLine] ||
- (**crt_TEH).selStart >= (**crt_TEH).lineStarts[bottomLine]) {
- for (theLine = 0; (**crt_TEH).selStart >= (**crt_TEH).lineStarts[theLine]; theLine++)
- ;
- SetCtlValue(vScroll, theLine - linesInFolder / 2);
- AdjustText();
- }
- }
-
- int SetUpMenus(void)
- {
- int i;
-
- myMenus[appleM] = NewMenu(appleID, "\p\024");
- AppendMenu(myMenus[appleM], "\pAbout ...");
- AddResMenu(myMenus[appleM], 'DRVR');
- myMenus[fileM] = NewMenu(fileID, "\pFile");
- AppendMenu(myMenus[fileM], "\pQuit/Q");
- myMenus[editM] = NewMenu(editID, "\pEdit");
- AppendMenu(myMenus[editM], "\pUndo/Z;-;Cut/X;Copy/C;Paste/V;Clear;Select All/A");
- DisableItem(myMenus[editM], undoCommand);
- for ((i=appleM); (i<=editM); i++)
- InsertMenu(myMenus[i], 0) ;
- DrawMenuBar();
- }
-
- int DoCommand(long mResult)
- {
- int theItem;
- Str255 name;
-
- theItem = LoWord(mResult);
- switch (HiWord(mResult))
- {
- case appleID:
- if (myMenus[appleM])
- {
- GetItem(myMenus[appleM], theItem, name);
- OpenDeskAcc(name);
- SetPort(crt_myWindow);
- break;
- }
- case fileID:
- ExitToShell();
- break;
-
- case editID:
- if (SystemEdit(theItem-1) == 0) {
- switch (theItem) {
- case cutCommand:
- ZeroScrap();
- TECut(crt_TEH);
- break;
-
- case copyCommand:
- ZeroScrap();
- TECopy(crt_TEH);
- break;
-
- case pasteCommand:
- TEPaste(crt_TEH);
- break;
-
- case clearCommand:
- TEDelete(crt_TEH);
- break;
-
- case selallCommand:
- TESetSelect(0, 32767, crt_TEH);
- break;
- }
- ShowSelect();
- }
- break;
- }
- HiliteMenu(0);
- return(1);
- }
-
- int MaintainCursor(void)
- {
- Point pt;
- WindowPeek wPtr;
- GrafPtr savePort;
-
- if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
- GetPort(&savePort);
- SetPort((GrafPtr)wPtr);
- GetMouse(&pt);
- if (PtInRect(pt, &(**crt_TEH).viewRect ) )
- SetCursor( *GetCursor(1));
- else SetCursor(&qd.arrow);
- SetPort(savePort);
- }
- }
-
- void test_inited(Str255 name)
- {
- if (!qd.thePort)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitCursor();
- SetUpMenus();
- }
- if (!crt_myWindow)
- {
- SetUpWindows(name);
- }
- }
-
- int cwrite(int fd, const void *buf, int size)
- {
- int tot;
- Ptr newtext;
- test_inited(fd==2?"\pStdErr":"\pStdOut");
- newtext = NewPtr(size);
- if (!newtext) newtext = (Ptr)buf;
- else for (tot = 0; tot < size; tot++)
- {
- newtext[tot] = ((char *)buf)[tot];
- if (newtext[tot]=='\n') newtext[tot]='\r';
- }
- TESetSelect(32767,32767,crt_TEH);
- TEInsert(newtext, size, crt_TEH);
- if (newtext != (Ptr) buf) DisposPtr(newtext);
- return size;
- }
-
- int cgetc(void)
- {
- EventRecord myEvent;
- WindowPtr whichWindow;
- short windowPart;
- Rect r;
- test_inited("\pStdIn");
- for (;;)
- {
- MaintainCursor();
- SystemTask();
- TEIdle(crt_TEH);
- if (GetNextEvent(everyEvent, &myEvent))
- {
- switch (myEvent.what)
- {
- case mouseDown:
- windowPart = FindWindow(myEvent.where, &whichWindow);
- DoMouseDown(windowPart, whichWindow, &myEvent);
- break;
- case keyDown:
- case autoKey:
- {
- register unsigned char theChar;
- theChar = myEvent.message & charCodeMask;
- if ((myEvent.modifiers & cmdKey) != 0)
- DoCommand(MenuKey(theChar));
- else if (theChar == '\r')
- return '\n';
- else if (theChar < ' ')
- {
- TEKey(theChar, crt_TEH);
- ShowSelect();
- }
- else return(theChar);
- break;
- }
- case activateEvt:
- if (ours((WindowPtr)myEvent.message))
- {
- if (myEvent.modifiers & activeFlag)
- {
- TEActivate(crt_TEH);
- ShowControl(vScroll);
- }
- else
- {
- TEDeactivate(crt_TEH);
- HideControl(vScroll);
- }
- }
- break;
-
- case updateEvt:
- if (ours((WindowPtr) myEvent.message))
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(crt_myWindow);
-
- BeginUpdate(crt_myWindow);
- EraseRect(&crt_myWindow->portRect);
- DrawControls(crt_myWindow);
- DrawGrowIcon(crt_myWindow);
- TEUpdate(&crt_myWindow->portRect, crt_TEH);
- EndUpdate(crt_myWindow);
-
- SetPort(savePort);
- }
-
- break;
- } /* end of case myEvent.what */
- } /* if */
- }
- }
-